/* 
 * @Author: 21克的爱情
 * @Date: 2020-09-28 16:35:15
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-09-28 16:53:28
 * @Description: 
 */
import 'package:encrypt/encrypt.dart';
import 'package:encrypt/encrypt_io.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'dart:async';
import 'package:flutter/services.dart' show rootBundle;

final parser = RSAKeyParser();

class EncryptHelper {

  static Future<String> encode(String src) async {
    String publicKeyString = await rootBundle.loadString('assets/keys/ki5k8_public_key.pem');
    RSAPublicKey publicKey = parser.parse(publicKeyString);
    final encrypter = Encrypter(RSA(publicKey: publicKey));
    return encrypter.encrypt(src).base64;

  }

  static Future<String> decode(String decoded) async {
    String privateKeyString = await rootBundle.loadString('assets/keys/ki5k8_private_key.pem');

    final privateKey = parser.parse(privateKeyString);

    final encrypter = Encrypter(RSA(privateKey: privateKey));
    return encrypter.decrypt(Encrypted.fromBase64(decoded));
  }

}